﻿namespace ACCOUNTING
{
    using ACCOUNTING.LVY.Business;
    using C1.Win.C1FlexGrid;
    using C1.Win.C1FlexGrid.Util.BaseControls;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    internal class MultiColumnDropDown : Form, ISupportInitialize
    {
        private static List<WeakReference> __ENCList = new List<WeakReference>();
        [AccessedThroughProperty("_flex")]
        private C1.Win.C1FlexGrid.C1FlexGrid __flex;
        private int _col;
        private C1.Win.C1FlexGrid.C1FlexGrid _ctl;
        private int _row;
        private IContainer components;
        private int mColValue;
        private DataTable mDataSource;
        private int mMaxrows;
        private int mShowTitle;
        private string[] mstrDataInit;
        private string[] mstrDataReturn;
        private string[] mTitle;
        private string mValue;

        public MultiColumnDropDown()
        {
            base.Deactivate += new EventHandler(this._flex_Click);
            base.PreviewKeyDown += new PreviewKeyDownEventHandler(this.MultiColumnDropDown_PreviewKeyDown);
            List<WeakReference> list = __ENCList;
            lock (list)
            {
                __ENCList.Add(new WeakReference(this));
            }
            this.mMaxrows = 10;
            this.mColValue = 0;
            this.mDataSource = new DataTable();
            this.mShowTitle = 0;
            this.InitializeComponent();
        }

        internal MultiColumnDropDown(C1.Win.C1FlexGrid.C1FlexGrid grid, int row, int col)
        {
            base.Deactivate += new EventHandler(this._flex_Click);
            base.PreviewKeyDown += new PreviewKeyDownEventHandler(this.MultiColumnDropDown_PreviewKeyDown);
            List<WeakReference> list = __ENCList;
            lock (list)
            {
                __ENCList.Add(new WeakReference(this));
            }
            this.mMaxrows = 10;
            this.mColValue = 0;
            this.mDataSource = new DataTable();
            this.mShowTitle = 0;
            this._ctl = grid;
            this._row = row;
            this._col = col;
            this._flex = new C1.Win.C1FlexGrid.C1FlexGrid();
            this.BeginInit();
            this._flex.Dock = DockStyle.Fill;
            this._flex.BorderStyle = C1.Win.C1FlexGrid.Util.BaseControls.BorderStyleEnum.FixedSingle;
            this._flex.SelectionMode = SelectionModeEnum.Row;
            this._flex.FocusRect = FocusRectEnum.None;
            this._flex.AllowEditing = false;
            this._flex.AutoSearch = AutoSearchEnum.FromCursor;
            this._flex.Cols.Fixed = 0;
            this._flex.KeyActionTab = KeyActionEnum.MoveDown;
            this._flex.TabIndex = 0;
            this._flex.Font = grid.Font;
            this._flex.Styles = grid.Styles;
            this._flex.Cols[0].Visible = false;
            this._flex.Rows.Fixed = 0;
            this.EndInit();
            this._flex.Row = -1;
            this.Controls.Add(this._flex);
            this.FormBorderStyle = FormBorderStyle.None;
            this.StartPosition = FormStartPosition.Manual;
            Rectangle r = grid.GetCellRect(row, col, true);
            r = grid.RectangleToScreen(r);
            Point point = new Point(r.Left, r.Bottom);
            this.Location = point;
            this.ShowInTaskbar = false;
            this.ShowIcon = false;
        }

        private void _flex_Click(object sender, EventArgs e)
        {
            this.Accept();
            this.Close();
        }

        private void _flex_KeyDown(object sender, KeyEventArgs e)
        {
            switch (((int) e.KeyCode))
            {
                case 13:
                    if ((this._flex.Row < 0) & (this._flex.Rows.Count > 1))
                    {
                        this._flex.Row = 0;
                    }
                    else
                    {
                        this.Accept();
                    }
                    break;

                case 0x1b:
                    e.Handled = true;
                    this._flex.Row = -1;
                    this.Close();
                    break;

                case 0x27:
                    e.Handled = true;
                    this.Accept();
                    break;

                case 40:
                    if ((this._flex.Row < 0) & (this._flex.Rows.Count > 1))
                    {
                        this._flex.Row = 0;
                    }
                    break;
            }
        }

        private void Accept()
        {
            string str = "";
            if (this._flex.Row > -1)
            {
                int num2 = this._flex.Cols.Count - 1;
                for (int i = 0; i <= num2; i++)
                {
                    if (str.Trim() == "")
                    {
                        str = Conversions.ToString(this._flex[this._flex.Row, i]);
                    }
                    else
                    {
                        str = Conversions.ToString(Microsoft.VisualBasic.CompilerServices.Operators.ConcatenateObject(str + "|", this._flex[this._flex.Row, i]));
                    }
                }
            }
            this.Close();
            this.mstrDataReturn = str.Split(new char[] { '|' });
        }

        public void BeginInit()
        {
            this._flex.BeginInit();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public void EndInit()
        {
            this._flex.EndInit();
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
            this.SuspendLayout();
            Size size = new Size(5, 13);
            this.AutoScaleBaseSize = size;
            size = new Size(0x128, 0x10d);
            this.ClientSize = size;
            this.KeyPreview = true;
            this.Name = "MultiColumnDropDown";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.Text = "MultiColumnDropDown";
            this.ResumeLayout(false);
        }

        private void MultiColumnDropDown_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            this.Close();
        }

        private void SetFormSize()
        {
            this._flex.AutoSizeCols(4);
            int num3 = Math.Min(this.mMaxrows, this._flex.Rows.Count);
            int count = this._flex.Cols.Count;
            int width = this._flex.Cols[count - 1].Right + 2;
            int height = (num3 * this._flex.Rows.DefaultSize) + 2;
            if (num3 < this._flex.Rows.Count)
            {
                width += SystemInformation.VerticalScrollBarWidth;
            }
            Size size = new Size(width, height);
            this.ClientSize = size;
        }

        private C1.Win.C1FlexGrid.C1FlexGrid _flex
        {
            [DebuggerNonUserCode]
            get
            {
                return this.__flex;
            }
            [MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode]
            set
            {
                KeyEventHandler handler = new KeyEventHandler(this._flex_KeyDown);
                EventHandler handler2 = new EventHandler(this._flex_Click);
                if (this.__flex != null)
                {
                    this.__flex.KeyDown -= handler;
                    this.__flex.Click -= handler2;
                }
                this.__flex = value;
                if (this.__flex != null)
                {
                    this.__flex.KeyDown += handler;
                    this.__flex.Click += handler2;
                }
            }
        }

        public int ColValue
        {
            get
            {
                return this.mColValue;
            }
            set
            {
                this.mColValue = value;
            }
        }

        public string[] DataInit
        {
            get
            {
                return this.mstrDataInit;
            }
            set
            {
                this.mstrDataInit = value;
                if (this.mstrDataInit.Length != 0)
                {
                    this._flex.Rows.Count = this.mstrDataInit.Length;
                    int num3 = this.mstrDataInit.Length - 1;
                    for (int i = 0; i <= num3; i++)
                    {
                        string[] strArray = this.mstrDataInit[i].Split(new char[] { '|' });
                        this._flex.Cols.Count = strArray.Length;
                        int num4 = strArray.Length - 1;
                        for (int j = 0; j <= num4; j++)
                        {
                            this._flex[i, j] = strArray[j];
                        }
                    }
                }
                this.SetFormSize();
                this._flex.ExtendLastCol = true;
            }
        }

        public DataTable DataSource
        {
            get
            {
                return this.mDataSource;
            }
            set
            {
                this.mDataSource = value;
                if (this.mDataSource != null)
                {
                    this._flex.DataSource = this.mDataSource;
                    this._flex.Cols[this.mColValue].Visible = false;
                    this.SetFormSize();
                    this._flex.ExtendLastCol = true;
                }
            }
        }

        public int MaxRow
        {
            get
            {
                return this.mMaxrows;
            }
            set
            {
                this.mMaxrows = value;
            }
        }

        public string[] ReturnData
        {
            get
            {
                return this.mstrDataReturn;
            }
            set
            {
                this.mstrDataReturn = value;
            }
        }

        public int ShowTitle
        {
            get
            {
                return this.mShowTitle;
            }
            set
            {
                this.mShowTitle = value;
            }
        }

        public string[] Title
        {
            get
            {
                return this.mTitle;
            }
            set
            {
                this.mTitle = value;
            }
        }

        public string Value
        {
            get
            {
                return this.mValue;
            }
            set
            {
                this.mValue = value;
                if (modGeneral.adjustNullString(this.mValue).Trim() != "")
                {
                    int num2 = this._flex.Rows.Count - 1;
                    for (int i = 1; i <= num2; i++)
                    {
                        if (this._flex.GetDataDisplay(i, this.ColValue) == this.mValue)
                        {
                            this._flex.Row = i;
                            this._flex.Select(i, 1);
                        }
                    }
                }
            }
        }
    }
}

